const bcrypt = require('bcryptjs')
const { generateToken, verifyToken } = require('./middleware/auth')
const config = require('./config')

async function testAuth () {
  try {
    console.log('🔧 测试认证功能...')

    // 1. 测试密码加密
    const password = '123456'
    const hashedPassword = await bcrypt.hash(
      password,
      config.security.bcryptSaltRounds
    )
    console.log('✅ 密码加密成功:', hashedPassword.substring(0, 20) + '...')

    // 2. 测试密码验证
    const isValid = await bcrypt.compare(password, hashedPassword)
    console.log('✅ 密码验证结果:', isValid)

    // 3. 测试JWT token生成
    const userId = 1
    const token = generateToken(userId)
    console.log('✅ Token生成成功:', token.substring(0, 50) + '...')

    // 4. 测试JWT token验证
    const decoded = verifyToken(token)
    console.log('✅ Token验证成功:', decoded)

    // 5. 测试无效token
    const invalidDecoded = verifyToken('invalid.token.here')
    console.log('✅ 无效token验证结果:', invalidDecoded)

    console.log('\n🎉 认证功能测试完成！')
  } catch (error) {
    console.error('❌ 认证测试失败:', error)
  }
}

testAuth()
